package com.machine.filling.function.set;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseActivity;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.init.OilGunList.OilGunListFragment;
import com.machine.filling.function.init.activity.MainFrameActivity;
import com.machine.filling.function.set.excOrderDetail.ExcOrderDetailFragment;
import com.machine.filling.function.set.orderDetail.OrderDetailFragment;
import com.machine.filling.function.set.orderList.OrderListFragment;
import com.machine.filling.function.set.versionCheck.VersionCheckFragment;

/**
 * Created by Administrator on 2018/3/10 0010.
 */

public class SetActivity extends BaseActivity<SetContract.Presenter> implements SetContract.View {

    private FragmentManager fm;
    private Fragment mContentFragment;

    @Override
    public View initViews() {
        setContentView(R.layout.activity_set);
        FrameLayout framelayout = (FrameLayout) findViewById(R.id.framelayout);
        return null;
    }

    @Override
    public void initDatas() {
        fm = getSupportFragmentManager();
        String showClassName = getIntent().getStringExtra("showClassName");
        if (OrderListFragment.class.getName().equals(showClassName)) {
            showOrderList(new Intent());
        } else {
            showSet(new Intent());
        }
    }

    /**
     * 显示系统设置
     */
    private void showSet(Intent intentPar) {
        final SetFragment setFragment = new SetFragment(intentPar);
        setFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {
                if (className.equals(VersionCheckFragment.class.getName())) {
                    showVersionCheck(intent);
                } else if (className.equals(OrderListFragment.class.getName())) {
                    showOrderList(intent);
//                    Intent intent2 = new Intent(mActivity, MainFrameActivity.class);
//                    intent2.putExtra("isFromSet", true);
//                    intent2.putExtra("isFillerLogin", true);//是否是加油员登录模式
//                    setFragment.getActivity().startActivityForResult(intent,SetFragment.REQ_ACT_CHECK_ORDER);

                } else if (className.equals(OilGunListFragment.class.getName())) {
                    Intent intent2 = new Intent(mActivity, MainFrameActivity.class);
                    intent2.putExtra("isFromSet", true);
                    startActivity(intent2);
                    finish();
                }
            }
        });
        replaceFragment(setFragment);
        mContentFragment = setFragment;
    }


    /**
     * 展示版本更新
     */
    private void showVersionCheck(final Intent intentPar) {
        VersionCheckFragment versionCheckFragment = new VersionCheckFragment(intentPar);
        versionCheckFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {
                if (className.equals(SetFragment.class.getName())) {
                    showSet(intent);
                }
            }
        });
        replaceFragment(versionCheckFragment);
        mContentFragment = versionCheckFragment;
    }

    /**
     * 显示订单列表查询列表
     */
    private void showOrderList(final Intent intentPar) {
        OrderListFragment orderListFragment = new OrderListFragment(intentPar);
        orderListFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {

                if (className.equals(ExcOrderDetailFragment.class.getName())) {
                    showExcOrderDetail(intent);
                } else if (className.equals(OrderDetailFragment.class.getName())) {
                    showOrderDetail(intent);
                } else if (className.equals(SetFragment.class.getName())) {
                    showSet(intent);
                }
            }
        });
        addFragment(mContentFragment, orderListFragment, orderListFragment.getClass());
        mContentFragment = orderListFragment;
    }

    /**
     * 显示订单详情
     */
    @Deprecated
    private void showOrderDetail(final Intent intentPar) {
        OrderDetailFragment orderDetailFragment = new OrderDetailFragment(intentPar);
        orderDetailFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {
                if (className.equals(OrderListFragment.class.getName())) {
                    showOrderList(intent);
                }
            }
        });
        addFragment(mContentFragment, orderDetailFragment, orderDetailFragment.getClass());
        mContentFragment = orderDetailFragment;
    }

    /**
     * 显示异常订单详情
     */
    private void showExcOrderDetail(final Intent intentPar) {
        ExcOrderDetailFragment excOrderDetailFragment = new ExcOrderDetailFragment(intentPar);
        excOrderDetailFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {
                if (className.equals(OrderListFragment.class.getName())) {
                    showOrderList(intent);
                }
            }
        });
        addFragment(mContentFragment, excOrderDetailFragment, excOrderDetailFragment.getClass());
        mContentFragment = excOrderDetailFragment;
    }

    /**
     * 更换fragment
     *
     * @param fragment
     */
    private void replaceFragment(Fragment fragment) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.framelayout, fragment).commit();
    }

    /**
     * 切换fragment
     *
     * @param from          当前fragment
     * @param to            将要显示的fragment
     * @param fragmentClass
     */
    private void addFragment(Fragment from, Fragment to, Class<? extends Fragment> fragmentClass) {
        if (mContentFragment != to) {
            mContentFragment = to;
            FragmentTransaction transaction = fm.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(from)
                        .add(R.id.framelayout, to, fragmentClass.getName()).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        initDatas();
    }
}
